package com.changpay.zywy.ui.activity.mine

import android.content.Intent
import android.text.TextUtils
import android.view.View
import com.changpay.zywy.R
import com.changpay.zywy.base.BaseActivity
import com.changpay.zywy.base.IView
import com.changpay.zywy.constant.Constant
import com.changpay.zywy.ext.showToast
import com.changpay.zywy.ext.sss
import com.changpay.zywy.http.RetrofitHelper
import com.changpay.zywy.utils.Preference
import kotlinx.android.synthetic.main.activity_get_code.*
import kotlinx.android.synthetic.main.title_layout.*

class GetCodeActivity : BaseActivity(), IView {

    private lateinit var type: String

    /**
     * local username
     */
    private var user: String by Preference(Constant.USERNAME_KEY, "")

    override fun showLoading() {

    }

    override fun hideLoading() {

    }

    override fun showDefaultMsg(msg: String) {
        showToast(msg)
    }

    override fun showMsg(msg: String) {
        showToast(msg)
    }

    override fun showError(errorMsg: String) {
        showToast(errorMsg)
    }


    private val onClickListener = View.OnClickListener { view ->
        when (view.id) {
            R.id.iv_back -> finish()
            R.id.get_code_next -> sumbit()
            R.id.tv_get_code -> getCode()
//            R.id.setting_three->
//            R.id.setting_four->
//            R.id.setting_five->
//            R.id.setting_six->
//            R.id.setting_out_login->
        }
    }

    private fun sumbit() {
        val phone = get_code_phone.getInputText()
        val code = get_code_code.getInputText()
        if (TextUtils.isEmpty(phone)) {
            showToast("请输入手机号")
            return
        }
        if (TextUtils.isEmpty(code)) {
            showToast("请输入验证码")
            return
        }
        val params = HashMap<String, String>()
        params["key"] = token
        params["phone"] = phone
        params["type"] = type
        params["captcha"] = code
        RetrofitHelper.service.check_sms_captcha(params).sss(this)
        {
            val intent = Intent(this, PayPasswordChangeActivity::class.java)
            intent.putExtra("activity_title", "修改支付密码")
            intent.putExtra("message", "第一步输入新的支付密码")
            intent.putExtra("title", "修改支付密码")
            intent.putExtra("imp_type", 0)
            intent.putExtra("captcha", code)
            startActivity(intent)
            finish()
        }
    }

    override fun attachLayoutRes(): Int {
        return R.layout.activity_get_code
    }

    override fun initData() {

    }

    override fun initView() {
        type = intent.getStringExtra("captcha_type")
        tv_title.text = intent.getStringExtra("title")
        iv_back.setOnClickListener(onClickListener)
        get_code_next.setOnClickListener(onClickListener)
        get_code_code.setCodeOnclickListe(onClickListener)
        get_code_phone.setInputText(user)
    }

    override fun start() {

    }


    /**
     * 获取验证码
     */
    private fun getCode() {
        val phone = get_code_phone.getInputText()
        if (TextUtils.isEmpty(phone)) {
            showToast("请输入手机号码")
            return
        }
        val param = hashMapOf<String, String>()
        param["phone"] = phone
        param["type"] = type
        RetrofitHelper.service.get_sms_captcha(param).sss(this) {
            showToast("发送验证码成功")
            get_code_code.starCode()
        }
    }

}
